package com.wxl.www.wxl.shiro.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxl.www.wxl.shiro.entity.SysPermission;
import com.wxl.www.wxl.shiro.entity.UserInfo;
import com.wxl.www.wxl.shiro.service.SysPermissionService;
import com.wxl.www.wxl.shiro.utils.CreateUUID;
import com.wxl.www.wxl.shiro.utils.DataUtil;
import com.wxl.www.wxl.shiro.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/permission")
public class SysPermissionController extends BaseController{

    @Autowired
    private SysPermissionService sysPermissionService;

    @RequestMapping("/list")
    public String index(Model model){
        return "sys/meun/index";
    }

    @PostMapping("/list")
    @ResponseBody
    public String menu(Model model){
        return JSON.toJSONString(sysPermissionService.getTreeMenuAll());
    }
    @PostMapping("/listData")
    @ResponseBody
    public IPage<SysPermission> listDate(String id,            @RequestParam(value="pageNo", defaultValue="1")Integer pageNo
            , @RequestParam(value="pageSize", defaultValue="10") Integer pageSize
            , Model model){
        IPage<SysPermission> page = new Page<>(pageNo, pageSize);
        QueryWrapper<SysPermission> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("update_time");
        wrapper.eq("parent_id",id);
       return sysPermissionService.page(page,wrapper);
    }
    @RequestMapping("/inster")
    public String toInster(String parentId, Model model){
        model.addAttribute("parentId",parentId);
        return "sys/meun/inster";
    }

    @PostMapping("/inster")
    @ResponseBody
    public ResultUtil inster(SysPermission sysPermission){
        try {
            sysPermission.setId(CreateUUID.getUUID32());
//            sysPermission.setUpdateTime(DataUtil.getDate());
            boolean save = sysPermissionService.save(sysPermission);
            return new ResultUtil();
        }catch (Exception e){
            System.out.println(e);
        }
        return null;
    }
    @RequestMapping("/update")
    public String update(String id,Model model){
        SysPermission sysPermission = sysPermissionService.getById(id);
        model.addAttribute("sysPermission",sysPermission);
        return "sys/meun/update";
    }
    @PostMapping("/delete")
    @ResponseBody
    public ResultUtil delete(String id){
        sysPermissionService.removeById(id);
        return null;
    }
}
